﻿NAMFox.ns(function() { with (NAMFox) {

name("Trace Tests");

//////////////////////////////////////////////
// Helpers
//////////////////////////////////////////////

var consoleService = $("@mozilla.org/consoleservice;1").service("nsIConsoleService");

function verifyMessage(o) {
    consoleService.reset();
    
    $.getConfig().diagnostics.traceLevel = o.noLogTraceLevel;
    o.action();
    
    var messages = {};
    var messageCount = {};
    consoleService.getMessageArray(messages, messageCount);
    
    equals(0, messageCount.value, o.noLogMessage);
    
    $.getConfig().diagnostics.traceLevel = o.logTraceLevel;
    o.action();
    
    var messages = {};
    var messageCount = {};
    consoleService.getMessageArray(messages, messageCount);
    
    equals(o.messageCount, messageCount.value, o.logMessage);
    
    if (messages.value[0]) {
        o.validate(messages.value[0]);
    }
}

//////////////////////////////////////////////
// Test Cases
//////////////////////////////////////////////

test("assert logs nothing when assertion succeeds", function() {
    consoleService.reset();
    
    $.assert(true, "Hello.");
    
    var messages = {};
    var messageCount = {};
    consoleService.getMessageArray(messages, messageCount);
    
    equals(0, messageCount.value, "No messages should be logged when assertions hold true.");
});

test("assert logs failure when assertion fails", function() {
    consoleService.reset();
    
    $.getConfig().diagnostics.traceLevel = 0;
    $.assert(false, "Hello.");
    
    var messages = {};
    var messageCount = {};
    consoleService.getMessageArray(messages, messageCount);
    
    equals(1, messageCount.value, "One message should be logged when assertions hold true.");
    notEqual(-1, messages.value[0].message.indexOf("Hello"), "The message in the error log should contain 'Hello.'", "$trace_assertionLogsInfoMessageWhenConditionIsFalse");
});

test("$.error logs exceptions at correct trace levels", function() {
    verifyMessage({
        noLogMessage: "No messages should be logged when an error is logged and the tracing level is off.",
        noLogTraceLevel: 0,
        logMessage: "One message should be logged when an error is logged and the tracing level is error.",
        logTraceLevel: 1,
        messageCount: 2,
        
        action: function() {
            $.error(new Error("Log me if you can."), "");
        },
        
        validate: function(msg) {
            notEqual(-1, msg.message.indexOf("Log me if you can."), "The message in the error log should contain 'Log me if you can.'");
            var error = msg.QueryInterface($("nsIScriptError"));
            equals(error.flags, $("nsIScriptError").exceptionFlag, "The message in the error log should be an error.");
        }
    });
});

test("$.fail logs error message", function() {
    consoleService.reset();
    
    $.getConfig().diagnostics.traceLevel = 0;
    $.fail("Hello.");
    
    var messages = {};
    var messageCount = {};
    consoleService.getMessageArray(messages, messageCount);
    
    equals(1, messageCount.value, "One message should be logged when assertions hold true.");
    notEqual(-1, messages.value[0].message.indexOf("Hello"), "The message in the error log should contain 'Hello.'");
});

test("$.info logs messages at correct trace levels", function() {
    verifyMessage({
        noLogMessage: "No messages should be logged when an info message is logged and the tracing level is warn.",
        noLogTraceLevel: 2,
        logMessage: "One message should be logged when an info message is logged and the tracing level is info.",
        logTraceLevel: 3,
        messageCount: 1,
        
        action: function() {
            $.info("Hello.");
        },
        
        validate: function(msg) {
            notEqual(-1, msg.message.indexOf("NAMFox: INFO : Hello."), "The message in the error log should contain 'NAMFox: INFO : Hello.'");
        }
    });
});

test("$.warn logs exceptions at correct trace levels", function() {
    verifyMessage({
        noLogMessage: "No messages should be logged when an error is logged and the tracing level is error.",
        noLogTraceLevel: 1,
        logMessage: "One message should be logged when an error is logged and the tracing level is warn.",
        logTraceLevel: 2,
        messageCount: 2,
        
        action: function() {
            $.warn(new Error("Log me if you can."));
        },
        validate: function(msg) {
            notEqual(-1, msg.message.indexOf("Log me if you can."), "The message in the error log should contain 'Log me if you can.'");
            var error = msg.QueryInterface($("nsIScriptError"));
            equals(error.flags, $("nsIScriptError").warningFlag, "The message in the error log should be a warning.");
        }
    });
});

test("$.verbose logs messages at correct trace levels", function() {
    verifyMessage({
        noLogMessage: "No messages should be logged when an info message is logged and the tracing level is info.",
        noLogTraceLevel: 3,
        logMessage: "One message should be logged when an info message is logged and the tracing level is verbose.",
        logTraceLevel: 4,
        messageCount: 1,
        
        action: function() {
            $.verbose("Hello.");
        },
        validate: function(msg) {
            notEqual(-1, msg.message.indexOf("NAMFox: VERBOSE : Hello."), "The message in the error log should contain 'NAMFox: VERBOSE : Hello.'");
        }
    });
});

}});